package com.qingxin.store.bean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChartDataCalc {

	private List<String> ids = new ArrayList<>();
	private List<String> categories = new ArrayList<>();
	private List<BigDecimal> data = new ArrayList<>();
	private Map<String, Integer> categoriesMap = new HashMap<>();
	private BigDecimal total;
	
	
	public ChartDataCalc() {
		total = BigDecimal.ZERO;
	}
	
	
	public void addCategory(String id, String category){
		categoriesMap.put(id, categories.size());
		ids.add(id);
		categories.add(category);
		data.add(BigDecimal.ZERO);
	}
	
	
	public void addData(String id, BigDecimal value){
		Integer index = categoriesMap.get(id);
		if(index == null){
			return;
		}
		BigDecimal dv = data.get(index);
		dv = dv.add(value);
		data.set(index, dv);
		
		total = total.add(value);
	}
	
	
	public ChartData getChartData(){
		ChartData chartData = new ChartData();
		chartData.setCategories(categories);
		chartData.setData(data);
		chartData.setTotal(total);
		return chartData;
	}
	
	
}
